package com.huatai.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.StringUtil;
import com.huatai.datacenter.config.CustomDataModelException;
import com.huatai.datacenter.entity.datamodel.DataModelBaseEntity;
import com.huatai.datacenter.entity.datamodel.DataModelEntity;
import com.huatai.datacenter.entity.datamodel.DataModelTypeEntity;
import com.huatai.datacenter.mapper.DataModelTypeMapper;
import com.huatai.datacenter.service.DataModelService;
import com.huatai.datacenter.service.DataModelTypeService;
import com.huatai.datacenter.utils.DataModelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gusiyi
 * @version 1.0
 * @className DataModelTypeServiceImpl
 * @description 描述：数据模型类型service实现类
 * @date 2023/5/25 15:19
 */
@Service
public class DataModelTypeServiceImpl extends ServiceImpl<DataModelTypeMapper,DataModelTypeEntity> implements DataModelTypeService {

	@Autowired
	private DataModelService dataModelService;

	@Override
	public Page<DataModelTypeEntity> getModelTypeList(DataModelTypeEntity dataModelTypeEntity) {
		LambdaQueryWrapper<DataModelTypeEntity> dataModelTypeQuery = Wrappers.lambdaQuery();
		dataModelTypeQuery
			.eq(DataModelBaseEntity::getDeleted,false)
			.like(StringUtil.isNotBlank(dataModelTypeEntity.getName()),DataModelTypeEntity::getName,dataModelTypeEntity.getName())
			.orderByDesc(DataModelBaseEntity::getUpdateTime);
		Page<DataModelTypeEntity> page = new Page<>(dataModelTypeEntity.getPageIndex(),dataModelTypeEntity.getPageSize());
		return page(page, dataModelTypeQuery);
	}

	@Override
	@Transactional(rollbackFor = {Exception.class})
	public boolean updateModelType(DataModelTypeEntity dataModelTypeEntity) {
		LambdaQueryWrapper<DataModelEntity> dataModelQuery = Wrappers.lambdaQuery();
		dataModelQuery
			.eq(DataModelBaseEntity::getDeleted,false)
			.eq(DataModelEntity::getDataModelTypeId,dataModelTypeEntity.getId());
		DataModelEntity updateDataModel = new DataModelEntity();
		updateDataModel.setDataModelTypeName(dataModelTypeEntity.getName());
		dataModelService.update(updateDataModel,dataModelQuery);
		return updateById(dataModelTypeEntity);
	}

	@Override
	public boolean deleteByIds(List<DataModelTypeEntity> dataModelTypeEntitys) {
		List<DataModelTypeEntity> deleteList = new ArrayList<>(dataModelTypeEntitys.size());
		StringBuilder resultMsg = new StringBuilder();
		boolean deleteFlag = false;
		for (DataModelTypeEntity dataModelTypeEntity : dataModelTypeEntitys) {
			Long typeId = dataModelTypeEntity.getId();
			LambdaQueryWrapper<DataModelEntity> dataModelQuery = Wrappers.lambdaQuery();
			dataModelQuery
				.eq(DataModelEntity::getDeleted,false)
				.eq(DataModelEntity::getDataModelTypeId,typeId);
			long hasDataModelUsed = dataModelService.count(dataModelQuery);
			if (hasDataModelUsed>0){
				List<DataModelEntity> dataModelEntities = dataModelService.list(dataModelQuery);
				resultMsg.append(DataModelUtil.getDeleteFailedMsg("模型类型管理",dataModelEntities.get(0).getDataModelTypeName(),"模型管理",
					()->{
						StringBuilder msg = new StringBuilder();
						for (DataModelEntity dataModelEntity : dataModelEntities) {
								msg.append("\"")
									.append(dataModelEntity.getDataModelName())
									.append("\",");
							}
						return msg.substring(0,msg.length()-1);
				}));
				deleteFlag = true;
			}else {
				dataModelTypeEntity.setDeleted(true);
				deleteList.add(dataModelTypeEntity);
			}
		}
		if (deleteFlag){
			resultMsg.append("请先在模型管理中修改或删除以上模型后再进行模型类型删除。");
			throw new CustomDataModelException(resultMsg.toString());
		}
		return updateBatchById(deleteList);
	}

}
